package com.car.loan.project.xr.service.impl;

import com.car.loan.common.exception.loan.LoanParameterException;
import com.car.loan.common.utils.BeanUtils;
import com.car.loan.common.utils.SecurityUtils;
import com.car.loan.common.utils.StringUtils;
import com.car.loan.framework.web.domain.AjaxResult;
import com.car.loan.project.xr.domain.Customer;
import com.car.loan.project.xr.domain.Loan;
import com.car.loan.project.xr.domain.LoanCar;
import com.car.loan.project.xr.domain.LoanDetail;
import com.car.loan.project.xr.domain.LoanExt;
import com.car.loan.project.xr.domain.LoanStatus;
import com.car.loan.project.xr.mapper.CustomerMapper;
import com.car.loan.project.xr.mapper.LoanCarMapper;
import com.car.loan.project.xr.mapper.LoanDetailMapper;
import com.car.loan.project.xr.mapper.LoanExtMapper;
import com.car.loan.project.xr.mapper.LoanMapper;
import com.car.loan.project.xr.mapper.LoanStatusMapper;
import com.car.loan.project.xr.util.RedisDataUtil;
import com.car.loan.project.xr.util.bo.business.MakeLoansBo;
import com.car.loan.project.xr.util.bo.business.RepaymentInfoBo;
import com.car.loan.project.xr.util.enums.MakeLoansInfoStatus;
import com.car.loan.project.xr.util.vo.LoanMakeLoansDetailVo;
import io.swagger.annotations.ApiModelProperty;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author huangding
 * @description
 * @date 2019/12/22 21:51
 */
@Service
public class LoanMakeLoansService {

    @Autowired
    private LoanMapper loanMapper;


    @Autowired
    private LoanCarMapper loanCarMapper;

    @Autowired
    private LoanExtMapper loanExtMapper;

    @Autowired
    private LoanDetailMapper loanDetailMapper;


    @Autowired
    private LoanStatusMapper loanStatusMapper;

    @Autowired
    private CustomerMapper customerMapper;


    @Autowired
    private RedisDataUtil redisDataUtil;

    @Transactional(rollbackFor = Exception.class)
    public AjaxResult input(MakeLoansBo makeLoansBo) {
        long loanId = Long.parseLong(makeLoansBo.getLoanId());
        Optional<LoanStatus> optionalLoanStatus = loanStatusMapper.selectByLoanId(loanId);
        LoanStatus loanStatus = optionalLoanStatus
            .orElseThrow(() -> new LoanParameterException("该业务状态数据异常，请联系管理员"));

        Long makeLoansInfoStatus = loanStatus.getMakeLoansInfoStatus();
        if (MakeLoansInfoStatus.INIT.status != makeLoansInfoStatus) {
            return AjaxResult.error("当前状态不可提交");
        }
        loanStatus.setMakeLoansInfoStatus(makeLoansBo.getOperatorStatus());
        loanStatus.setMakeLoansInfoTime(LocalDateTime.now());
        loanStatus.setMortgageInfoId(SecurityUtils.getLoginUserId());
        loanStatusMapper.updateById(loanStatus);

        Optional<LoanDetail> optionalLoanDetail = loanDetailMapper.selectByLoanId(loanId);
        LoanDetail loanDetail = optionalLoanDetail.get();
        loanDetail.setContractFilingDate(LocalDate.now().toString());
        loanDetail.setLendDate(makeLoansBo.getLendDate());
        loanDetailMapper.updateById(loanDetail);

        Optional<LoanExt> optionalLoanExt = loanExtMapper.selectByLoanId(loanId);
        LoanExt loanExt = optionalLoanExt
            .orElseThrow(() -> new LoanParameterException("业务数据扩展信息异常"));
        loanExt.setFinancingAmount(makeLoansBo.getFinancingAmount());
        loanExtMapper.updateById(loanExt);

        return AjaxResult.success();
    }

    public LoanMakeLoansDetailVo detail(Long loanId) {

        Optional<Loan> optionalLoan
            = Optional.ofNullable(loanMapper.selectById(loanId));
        Loan loan = optionalLoan.orElseThrow(() -> new LoanParameterException("无该业务数据"));
        //获取数据源
        Optional<LoanCar> optionalLoanCar = loanCarMapper.selectByLoanId(loanId);
        LoanCar loanCar = optionalLoanCar
            .orElseThrow(() -> new LoanParameterException("业务数据车辆信息异常"));
        Optional<LoanExt> optionalLoanExt = loanExtMapper.selectByLoanId(loanId);
        LoanExt loanExt = optionalLoanExt
            .orElseThrow(() -> new LoanParameterException("业务数据扩展信息异常"));
        Optional<LoanDetail> optionalLoanDetail = loanDetailMapper.selectByLoanId(loanId);
        LoanDetail loanDetail = optionalLoanDetail
            .orElseThrow(() -> new LoanParameterException("业务数据详情信息异常"));



        Customer customer = customerMapper.selectById(loan.getCustomerId());
        LoanMakeLoansDetailVo loanMakeLoansDetailVo = new LoanMakeLoansDetailVo();
        loanMakeLoansDetailVo.setLoanNo(loan.getBusinessNo());
        loanMakeLoansDetailVo.setDealerId(loan.getDealerId());
        loanMakeLoansDetailVo.setName(customer.getName());
        loanMakeLoansDetailVo.setMobile(customer.getMobile());
        loanMakeLoansDetailVo.setIdCardNo(customer.getIdCardNo());

        loanMakeLoansDetailVo.setCarName(loanExt.getCarName());
        loanMakeLoansDetailVo.setCarType(loanCar.getCarType());
        loanMakeLoansDetailVo.setCarNature(loanCar.getCarNature());
        loanMakeLoansDetailVo.setPlateType(loanCar.getPlateType());
        loanMakeLoansDetailVo.setInstallmentAmount(loanExt.getInstallmentAmount());
        loanMakeLoansDetailVo.setFinancingAmount(loanExt.getFinancingAmount());
        loanMakeLoansDetailVo.setSellAmount(loanExt.getSellAmount());
        loanMakeLoansDetailVo.setTerm(loanExt.getTerm());
        loanMakeLoansDetailVo.setLendDate(loanDetail.getLendDate());



        loanMakeLoansDetailVo.setAdminName(redisDataUtil.getAdminName(loan.getAdminId()));
        loanMakeLoansDetailVo.setBankName(redisDataUtil.getBankName(loan.getBankId()));
        loanMakeLoansDetailVo.setDealerName(redisDataUtil.getDealerName(loan.getDealerId()));
        loanMakeLoansDetailVo.setDeptName(redisDataUtil.getDeptName(loan.getDeptId()));
        RepaymentInfoBo repaymentInfoBo = new RepaymentInfoBo();
        BeanUtils.copyBeanProp(repaymentInfoBo, loanDetail);

        String firstRepayDate = loanDetail.getFirstRepayDate();
        if (StringUtils.isNotEmpty(firstRepayDate)) {
            //获取账单日
            repaymentInfoBo.setStatementDay(LocalDate.parse(firstRepayDate).getDayOfMonth());
        }

        Optional<LoanStatus> optionalLoanStatus = loanStatusMapper.selectByLoanId(loanId);
        LoanStatus loanStatus = optionalLoanStatus
            .orElseThrow(() -> new LoanParameterException("该业务状态数据异常，请联系管理员"));
        repaymentInfoBo.setOperatorStatus(loanStatus.getRepaymentInfoStatus());
        repaymentInfoBo.setLoanId(loan.getId().toString());
        loanMakeLoansDetailVo.setRepaymentInfo(repaymentInfoBo);

        MakeLoansBo makeLoansBo = new MakeLoansBo();
        makeLoansBo.setLoanId(loan.getId().toString());
        makeLoansBo.setOperatorStatus(loanStatus.getMakeLoansInfoStatus());
        makeLoansBo.setLendDate(loanDetail.getLendDate());
        makeLoansBo.setFinancingAmount(loanExt.getFinancingAmount());

        return loanMakeLoansDetailVo;
    }


}
